<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>ES5写法</title>
</head>

<body>
    <script>
        //以函数的形式来写对象
        function Person(name, age) {
            //把属性加到对象上
            this.name = name;
            this.age = age;
            /*
              第一种
             给对象添加方法
             this.Sayname = function () {
                 console.log("姓名为:" + this.name);
             }
             */
        }

        //第二种
        //给对象原型上添加方法
        Person.prototype.Showname = function () {
            console.log("姓名为:" + this.name);
        }
        Person.prototype.Showage = function () {
            console.log("年龄为:" + this.age);
        }


        let person = new Person("邓超", 35);
        person.Showname()
        person.Showage()
        console.log("以下为继承方式:")
        //使用ES5进行继承
        function Worker(name, age, job) {
            //把person的类的指向转到Worker中,Worker一旦执行,person的东西会成worker的
            //指向this,因为是Worker执行调用,也就是把this指向了Worker
            Person.call(this, name, age);
            //Worker自己的属性
            this.job = job;
        }
        //因为this已经指向了person,必须得初始化一下
        //在worker的原型上初始化了Person实例
        Worker.prototype = new Person();
        //初始化完毕后再打回worker就完成了"假"继承
        //然后把Worker的prototype的构造器打回到worker,完成了继承
        Worker.prototype.constructor = Worker;
        Worker.prototype.Showjob = function () {
            alert("工作为:" + this.job)
        }
        //初始化一个Worker实例
        let w = new Worker("邓超",35,"演员");
        w.Showname();
        w.Showage();
        w.Showjob();
        //冻结对象,使得对象无法被修改
        let p = {
            name:'fjy',
            age:19
        }
        let people = Object.freeze(p);
    </script>
</body>

</html>